package com.cy.store.config;

import com.cy.store.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Luo Sheng
 * @school YiBin university
 * @create 2022-12-01 10:44
 */
@Configuration // 将当前类加载到配置中
/**
 * 处理器拦截器的注册
 */
public class LoginInterceptorConfigurer implements WebMvcConfigurer {
    // 创建自定义拦截器
    HandlerInterceptor interceptor = new LoginInterceptor();


    /**
     * 配置拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 配置白名单
        List<String> patterns = new ArrayList<>();
        patterns.add("/bootstrap3/**");
        patterns.add("/css/**");
        patterns.add("/images/**");
        patterns.add("/js/**");
        patterns.add("/web/register.html");
        patterns.add("/web/login.html");
        patterns.add("/web/product.html");
        patterns.add("/users/reg");
        patterns.add("/users/login");
//        patterns.add("/districts/**");
        // 完成拦截器的注册
        registry.addInterceptor(interceptor)
                .addPathPatterns("/**") // 要拦截的url是什么
                .excludePathPatterns(patterns);       // 要放行的url是什么
    }
}
